package deltax.common.respackage
{
    import deltax.common.error.*;
    import deltax.common.log.*;
    import deltax.common.resource.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;

    public class PackedResSetting extends Object
    {
        private var m_swfPackages:Dictionary;
        private var m_packedResInfos:Dictionary;
        private var m_resLoadStatusMap:Dictionary;
        private var m_packedResSettingLoaded:Boolean;
        private static var m_instance:PackedResSetting;

        public function PackedResSetting(param1:SingletonEnforcer)
        {
            if (m_instance)
            {
                throw SingletonMultiCreateError(PackedResSetting);
            }
            this.m_resLoadStatusMap = new Dictionary();
            this.m_packedResInfos = new Dictionary();
            this.m_swfPackages = new Dictionary();
            return;
        }// end function

        public function parseSetting(param1:XML) : void
        {
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_5:XML = null;
            var _loc_6:ResSettingItem = null;
            var _loc_2:* = param1.swf;
            for each (_loc_5 in _loc_2)
            {
                
                _loc_6 = new ResSettingItem();
                _loc_6.analyzeXML(_loc_5, this.m_packedResInfos);
                _loc_4 = _loc_6.swfUrl;
                this.m_swfPackages[_loc_4] = _loc_6;
            }
            this.m_packedResSettingLoaded = true;
            return;
        }// end function

        public function getResSettingItemByItemUrl(param1:String) : ResSettingItem
        {
            var _loc_2:* = Enviroment.convertURLForQueryPackage(param1);
            return this.m_packedResInfos[_loc_2];
        }// end function

        public function getSwfUrl(param1:String, param2:Boolean = false) : String
        {
            var _loc_3:* = Enviroment.convertURLForQueryPackage(param1);
            var _loc_4:* = this.m_packedResInfos[_loc_3];
            if (this.m_packedResInfos[_loc_3])
            {
                return param2 ? (_loc_4.versionedSwfUrl) : (_loc_4.swfUrl);
            }
            return null;
        }// end function

        public function checkAndReleaseAllLoadedPackages() : void
        {
            var _loc_1:ResSettingItem = null;
            for each (_loc_1 in this.m_swfPackages)
            {
                
                _loc_1.clearAllInnerFileLoadState();
            }
            return;
        }// end function

        public function checkAndReleaseSpecificLoadedPackages(param1:String) : void
        {
            var _loc_2:ResSettingItem = null;
            if (!param1)
            {
                this.checkAndReleaseAllLoadedPackages();
            }
            else
            {
                for each (_loc_2 in this.m_swfPackages)
                {
                    
                    if (_loc_2.swfUrl.indexOf(param1) >= 0)
                    {
                        _loc_2.clearAllInnerFileLoadState();
                    }
                }
            }
            return;
        }// end function

        public function get loaded() : Boolean
        {
            return this.m_packedResSettingLoaded;
        }// end function

        public function loadPackedResSettingSWF(param1:String, param2:String, param3:Loader = null) : void
        {
            var externalLoader:Boolean;
            var loaderContext:LoaderContext;
            var onXMLoaded:Function;
            var onXMLoadFailed:Function;
            var url:* = param1;
            var className:* = param2;
            var loader:* = param3;
            onXMLoaded = function (event:Event) : void
            {
                var cls:Class;
                var packedSettingXml:XML;
                var event:* = event;
                onPackedResSettingLoadedOrFailed(true);
                try
                {
                    cls = loaderContext.applicationDomain.getDefinition(className) as Class;
                }
                catch (e:ReferenceError)
                {
                    cls;
                    dtrace(LogLevel.FATAL, e.message);
                }
                if (cls)
                {
                    packedSettingXml = XML(new cls);
                    parseSetting(packedSettingXml);
                }
                DownloadStatistic.instance.addDownloadedBytes(loader.contentLoaderInfo.bytesLoaded, url);
                return;
            }// end function
            ;
            onXMLoadFailed = function (event:Event) : void
            {
                onPackedResSettingLoadedOrFailed(false);
                m_packedResSettingLoaded = false;
                return;
            }// end function
            ;
            var onPackedResSettingLoadedOrFailed:* = function (param1:Boolean) : void
            {
                loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onXMLoaded);
                loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onXMLoadFailed);
                if (!externalLoader)
                {
                    loader.unloadAndStop(false);
                }
                return;
            }// end function
            ;
            if (this.m_packedResSettingLoaded)
            {
                return;
            }
            externalLoader = loader != null;
            if (!loader)
            {
                loader = new Loader();
            }
            this.m_packedResSettingLoaded = true;
            loaderContext = new LoaderContext();
            loaderContext.applicationDomain = new ApplicationDomain();
            loader.load(new URLRequest(url), loaderContext);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onXMLoaded);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onXMLoadFailed);
            return;
        }// end function

        public function loadPackedResSetting(param1:String, param2:URLLoader = null) : void
        {
            var onXMLoaded:Function;
            var onXMLoadFailed:Function;
            var url:* = param1;
            var urlLoader:* = param2;
            onXMLoaded = function (event:Event) : void
            {
                onPackedResSettingLoadedOrFailed(true);
                var _loc_2:* = XML(urlLoader.data);
                parseSetting(_loc_2);
                DownloadStatistic.instance.addDownloadedBytes(urlLoader.bytesTotal, url);
                m_packedResSettingLoaded = true;
                return;
            }// end function
            ;
            onXMLoadFailed = function (event:Event) : void
            {
                onPackedResSettingLoadedOrFailed(false);
                return;
            }// end function
            ;
            var onPackedResSettingLoadedOrFailed:* = function (param1:Boolean) : void
            {
                urlLoader.removeEventListener(Event.COMPLETE, onXMLoaded);
                urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onXMLoadFailed);
                return;
            }// end function
            ;
            if (this.m_packedResSettingLoaded)
            {
                return;
            }
            if (!urlLoader)
            {
                urlLoader = new URLLoader(new URLRequest(url));
            }
            else
            {
                urlLoader.load(new URLRequest(url));
            }
            urlLoader.addEventListener(Event.COMPLETE, onXMLoaded);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLoadFailed);
            return;
        }// end function

        public static function get instance() : PackedResSetting
        {
            var _loc_1:* = m_instance || new PackedResSetting(new SingletonEnforcer());
            m_instance = m_instance || new PackedResSetting(new SingletonEnforcer());
            return _loc_1;
        }// end function

    }
}

class SingletonEnforcer extends Object
{

    function SingletonEnforcer()
    {
        return;
    }// end function

}

